 package com.ruoyi.web.controller.project.educationreform;


 import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.domain.project.educationreform.EducationReform;
 import com.ruoyi.system.domain.project.educationreform.EducationReformMember;
 import com.ruoyi.system.service.project.IEducationReformMemberService;
 import com.ruoyi.system.service.project.IEducationReformService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;

 import javax.servlet.http.HttpServletResponse;
 import java.util.List;

 /**
  * 一流学科Controller
  *
  * @author ruoyi
  * @date 2025-08-28
  */
 @RestController
 @RequestMapping("/project/educationreform")
 public class EducationReformController extends BaseController
 {
     @Autowired
     private IEducationReformService firstClassService;
    @Autowired
    private IEducationReformMemberService firstClassMemberService;
     /**
      * 查询一流学科列表
      */
     @GetMapping("/list")
     public TableDataInfo list(EducationReform firstClass)
     {
         startPage();
         List<EducationReform> list = firstClassService.selectFirstClassList(firstClass);
         return getDataTable(list);
     }

     /**
      * 导出一流学科列表
      */
     @Log(title = "一流学科", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
     public void export(HttpServletResponse response, EducationReform firstClass,
                        @RequestParam(required = false) String exportFields)
     {
         List<EducationReform> list = firstClassService.selectFirstClassList(firstClass);
         ExcelUtil<EducationReform> util = new ExcelUtil<EducationReform>(EducationReform.class);

         // 如果传递了导出字段，则进行处理
         if (StringUtils.isNotBlank(exportFields)) {
             // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
             String[] fields = exportFields.split(",");
             util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
         }

         util.exportExcel(response, list, "一流学科数据");
     }

     /**
      * 获取一流学科详细信息
      */
     @GetMapping(value = "/{id}")
     public AjaxResult getInfo(@PathVariable("id") Long id)
     {
         return success(firstClassService.selectFirstClassById(id));
     }

     /**
      * 新增一流学科
      */
     @Log(title = "一流学科", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody EducationReform firstClass)
     {
         EducationReformMember ra = new EducationReformMember();
         if(firstClass.getId()!=null){
             ra.setClassId(firstClass.getId());
         }else{
             ra.setClassId(-1L);
         }
         List<EducationReformMember> firstClassMembers = firstClassMemberService.selectFirstClassMemberList( ra);
         Long userId = firstClass.getTeacherId() ;
         if(firstClassMembers!=null&&!firstClassMembers.isEmpty()){
             for(EducationReformMember firstClassMember:firstClassMembers){
                 if(firstClassMember.getMemberId().equals(userId)){
                     return error("此项目您已经参与并有名次记录，请勿重复添加");
                 }
             }
         }
         return toAjax(firstClassService.insertFirstClass(firstClass));
     }

     /**
      * 修改一流学科
      */
     @Log(title = "一流学科", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody EducationReform firstClass)
     {
         return toAjax(firstClassService.updateFirstClass(firstClass));
     }

     /**
      * 删除一流学科
      */
     @Log(title = "一流学科", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable Long[] ids)
     {
         return toAjax(firstClassService.deleteFirstClassByIds(ids));
     }

    /**
      * 根据关键字查询
      */
     @GetMapping("/getInfoByTitle")
      public AjaxResult getInfoByTitle(  String data){
         EducationReform ra = firstClassService.getInfoByTitle(data);
         return success(ra);
     }
 }
